package com.sccba.emtbusiness.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sccba.common.annotation.DataScope;
import com.sccba.common.exception.ServiceException;
import com.sccba.common.utils.DateUtils;
import com.sccba.common.utils.SecurityUtils;
import com.sccba.emtbusiness.domain.YjczEmergencyHandleProcess;
import com.sccba.emtbusiness.domain.YjczHandleTimeLine;
import com.sccba.emtbusiness.enums.NodeHandleStatus;
import com.sccba.emtbusiness.enums.ProductEventStatus;
import com.sccba.emtbusiness.service.IYjczEmergencyHandleProcessService;
import com.sccba.emtbusiness.service.IYjczHandleTimeLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczProductEventInfoMapper;
import com.sccba.emtbusiness.domain.YjczProductEventInfo;
import com.sccba.emtbusiness.service.IYjczProductEventInfoService;

/**
 * 生产事件Service业务层处理
 * 
 * @author lichh
 * @date 2024-04-22
 */
@Service
public class YjczProductEventInfoServiceImpl implements IYjczProductEventInfoService 
{
    @Autowired
    private YjczProductEventInfoMapper yjczProductEventInfoMapper;

    @Autowired
    private IYjczEmergencyHandleProcessService yjczEmergencyHandleProcessService;

    @Autowired
    private IYjczHandleTimeLineService yjczHandleTimeLineService;

    /**
     * 查询生产事件
     * 
     * @param pkId 生产事件主键
     * @return 生产事件
     */
    @Override
    public YjczProductEventInfo selectYjczProductEventInfoByPkId(Long pkId)
    {
        return yjczProductEventInfoMapper.selectYjczProductEventInfoByPkId(pkId);
    }

    /**
     * 查询生产事件列表
     * 
     * @param yjczProductEventInfo 生产事件
     * @return 生产事件
     */
    @Override
//    @DataScope(scopeByChannel = true, channelAlias = "c")
    public List<YjczProductEventInfo> selectYjczProductEventInfoList(YjczProductEventInfo yjczProductEventInfo)
    {
        return yjczProductEventInfoMapper.selectYjczProductEventInfoList(yjczProductEventInfo);
    }

    /**
     * 新增生产事件
     * 
     * @param yjczProductEventInfo 生产事件
     * @return 结果
     */
    @Override
    public int insertYjczProductEventInfo(YjczProductEventInfo yjczProductEventInfo)
    {
        yjczProductEventInfo.setEventState(ProductEventStatus.WAIT_DO.getCode());
        yjczProductEventInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        yjczProductEventInfo.setCreateTime(DateUtils.getNowDate());
        return yjczProductEventInfoMapper.insertYjczProductEventInfo(yjczProductEventInfo);
    }

    /**
     * 修改生产事件
     * 
     * @param yjczProductEventInfo 生产事件
     * @return 结果
     */
    @Override
    public int updateYjczProductEventInfo(YjczProductEventInfo yjczProductEventInfo)
    {
        yjczProductEventInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        yjczProductEventInfo.setUpdateTime(DateUtils.getNowDate());
        return yjczProductEventInfoMapper.updateYjczProductEventInfo(yjczProductEventInfo);
    }

    /**
     * 批量删除生产事件
     * 
     * @param pkIds 需要删除的生产事件主键
     * @return 结果
     */
    @Override
    public int deleteYjczProductEventInfoByPkIds(Long[] pkIds)
    {
        return yjczProductEventInfoMapper.deleteYjczProductEventInfoByPkIds(pkIds);
    }

    /**
     * 删除生产事件信息
     * 
     * @param pkId 生产事件主键
     * @return 结果
     */
    @Override
    public int deleteYjczProductEventInfoByPkId(Long pkId)
    {
        return yjczProductEventInfoMapper.deleteYjczProductEventInfoByPkId(pkId);
    }

    @Override
    public List<YjczHandleTimeLine> handleProductEvent(YjczProductEventInfo productEventInfo) {
        Long fbId = productEventInfo.getEventFbId();
        Long channelId = productEventInfo.getEventChannelId();
        Long sceneId = productEventInfo.getEventSceneId();
        if (null == fbId || null == channelId || null == sceneId) {
            throw new ServiceException("请先维护所属发版、应用系统、应急场景后再行处置");
        }
        Long eventId = productEventInfo.getPkId();
        List<YjczHandleTimeLine> handleTimeLines = yjczHandleTimeLineService.selectListByEventId(eventId);
        if (null != handleTimeLines && !handleTimeLines.isEmpty()) {
            Long handleSceneId = handleTimeLines.get(0).getHandleSceneId();
            if (handleSceneId == sceneId) {
                return handleTimeLines;
            }
            yjczHandleTimeLineService.deleteByEventId(eventId);
        }

        List<YjczEmergencyHandleProcess> handleProcessList = yjczEmergencyHandleProcessService.getListByFbChannelAndScene(fbId, channelId, sceneId);
        if (null == handleProcessList || handleProcessList.isEmpty()) {
            handleProcessList = yjczEmergencyHandleProcessService.selectProcessTemplateBySceneId(sceneId);
            if (null == handleProcessList || handleProcessList.isEmpty()) {
                throw new ServiceException("未查询到相关应急处置流程");
            }
        }
        handleTimeLines = new ArrayList<>();
        YjczHandleTimeLine handleTimeLine;
        for (YjczEmergencyHandleProcess process : handleProcessList) {
            handleTimeLine = YjczHandleTimeLine.transByHandleProcess(process);
            handleTimeLine.setHandleEventId(eventId);
            handleTimeLine.setHandleSceneId(sceneId);
            handleTimeLine.setHandleState(NodeHandleStatus.WAIT_DO.getCode());
            handleTimeLines.add(handleTimeLine);
        }
        yjczHandleTimeLineService.batchInsertTimeLine(handleTimeLines);
        return handleTimeLines;
    }

    @Override
    public void updateEventState(int state, Long eventId) {
        yjczProductEventInfoMapper.updateEventStateByPkId(state, eventId);
    }

    @Override
    public YjczProductEventInfo selectOneForSearch(Long pkId) {
        return yjczProductEventInfoMapper.selectOneByPkIdForSearch(pkId);
    }
}
